package com.khoayang.littlefighterchaos.menu;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.menus.CCMenu;
import org.cocos2d.menus.CCMenuItemImage;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor3B;

import android.content.Context;

import com.khoayang.littlefighterchaos.game.CharacterFactory;
import com.khoayang.littlefighterchaos.game.GameScene;

public class BoxImageCharacter extends CCLayer{

	private int _idCharacter;
	private int _maxID;
	private int _teamID;
	private int _maxTeamID;
	private Context _context;
	private CCSprite _represent;
	private String _charName;
	private CCMenuItemImage _leftButton;
	private CCMenuItemImage _rightButton;
	private CCMenuItemImage _leftButtonTeamID;
	private CCMenuItemImage _rightButtonTeamID;
	private CCLabel _nameCharacter;
	private CCLabel _labelTeamID;
	private CGPoint _position;
	private String _controler;
	private CCLabel _markPlayer;
	
	//CCMenuItemImage _playButton;
	
	public BoxImageCharacter(Context context, CGPoint position){
		
		_context = context;
		_idCharacter = 0;
		_maxTeamID = 8;
		set_teamID(1);
		
		
		set_position(position);
		
		CGSize winSize = CCDirector.sharedDirector().winSize();
		CharacterFactory characterFactory = CharacterFactory.getInstance(_context);
		_maxID = characterFactory.get_AllCharacter().size();
		
		CCSprite backGroundCharacter = CCSprite.sprite("main-menu/graybox.jpg");
		backGroundCharacter.setPosition(_position.x,_position.y-winSize.height/24f);
		backGroundCharacter.setOpacity(200);
		backGroundCharacter.setScaleX((winSize.width/6.42f)/backGroundCharacter.getContentSize().width);
		backGroundCharacter.setScaleY((winSize.height/3.05f)/backGroundCharacter.getContentSize().height);
		addChild(backGroundCharacter);

		_controler = "PLAYER";
		_markPlayer = CCLabel.makeLabel(get_controler(), "DroidSans", 20);
		_markPlayer.setColor(ccColor3B.ccBLACK);
		_markPlayer.setPosition(_position.x,_position.y+winSize.height/9.37f);
		addChild(_markPlayer);
		
		_leftButton = CCMenuItemImage.item("main-menu/LeftArrow.png","main-menu/LeftArrow.png", this, "reducedID");
		_rightButton = CCMenuItemImage.item("main-menu/RightArrow.png","main-menu/RightArrow.png", this, "increaseID");
		_leftButton.setScale((winSize.height/16f)/_leftButton.getContentSize().height);
		_rightButton.setScale((winSize.height/16f)/_rightButton.getContentSize().height);
		CCMenu chooseCharacter = CCMenu.menu(_leftButton, _rightButton);
		_leftButton.setPosition(-winSize.width/15.83f, -winSize.height/8.26f);
		_rightButton.setPosition(+winSize.width/15.83f,-winSize.height/8.26f);
		chooseCharacter.setPosition(get_position());
		
		if(_idCharacter < _maxID)
		{
		set_charName(characterFactory.get_AllCharacter().get(_idCharacter).get_State().get_Name());	
		_nameCharacter = CCLabel.makeLabel(get_charName(), "DroidSans", 20);
		_nameCharacter.setColor(ccColor3B.ccBLACK);
		_nameCharacter.setPosition(get_position().x, get_position().y-winSize.height/8.26f);
		
		_represent = CCSprite.sprite("character/representImage/" +get_charName() + "_f.bmp");
		_represent.setScale((winSize.height/5.6f)/_represent.getContentSize().height);
		_represent.setPosition(get_position().x,get_position().y);
		}
		else
		{
			_charName = "";
			_nameCharacter.setString("");
		}
		//m.alignItemsVertically();
		addChild(chooseCharacter,4);
		addChild(_nameCharacter,5);
		addChild(_represent,3);
		
		if(MainMenuLayer._isStageMode == false)
		{
		_leftButtonTeamID = CCMenuItemImage.item("main-menu/LeftArrow.png","main-menu/LeftArrow.png", this, "reducedTeamID");
		_rightButtonTeamID = CCMenuItemImage.item("main-menu/RightArrow.png","main-menu/RightArrow.png", this, "increaseTeamID");
		_leftButtonTeamID.setScale((winSize.height/16f)/_leftButton.getContentSize().height);
		_rightButtonTeamID.setScale((winSize.height/16f)/_rightButton.getContentSize().height);
		CCMenu chooseTeam = CCMenu.menu(_leftButtonTeamID, _rightButtonTeamID);
		_leftButtonTeamID.setPosition(-winSize.width/15.83f, -winSize.height/5.72f);
		_rightButtonTeamID.setPosition(+winSize.width/15.83f,-winSize.height/5.72f);
		chooseTeam.setPosition(get_position());
		
		_labelTeamID = CCLabel.makeLabel("Team " + get_teamID(), "DroidSans", 20);
		_labelTeamID.setColor(ccColor3B.ccBLACK);
		_labelTeamID.setPosition(get_position().x, get_position().y-winSize.height/5.72f);
		
		addChild(chooseTeam,6);
		addChild(_labelTeamID,7);
		}
		
	}
	
	public void playGame(Object sender)
	{
		GameScene gameScene = new GameScene(_context);
        CCDirector.sharedDirector().runWithScene(gameScene);
	}
	
	public void reducedID(Object sender)
	{
		
		_idCharacter--;
		if(_idCharacter < 0)
		{
			_idCharacter = _maxID ;
		}
		
		changeCharater();
	}
	
	public void increaseID(Object sender)
	{
		_idCharacter++;
		if(_idCharacter > _maxID)
		{
			_idCharacter = 0;
		}
		changeCharater();
	}
	
	public void reducedTeamID(Object sender)
	{
		
		set_teamID(get_teamID() - 1);
		if(get_teamID() < 1)
		{
			set_teamID(_maxTeamID);
		}
		
		changeTeamID();
	}
	
	public void increaseTeamID(Object sender)
	{
		set_teamID(get_teamID() + 1);
		if(get_teamID() > _maxTeamID)
		{
			set_teamID(1);
		}
		changeTeamID();
	}
	
	public void changeTeamID() {
		
		_labelTeamID.setString("Team " + get_teamID());
	}

	public void setIdCharacter(int idCharater)
	{
		_idCharacter = idCharater;
	}
	
	public void changeCharater(){
	
		this.removeChild(_represent, true);
		if(_idCharacter < _maxID)
		{
		CharacterFactory characterFactory = CharacterFactory.getInstance(_context);
		
		set_charName(characterFactory.get_AllCharacter().get(_idCharacter).get_State().get_Name());
		_nameCharacter.setString(get_charName());
		_represent = CCSprite.sprite("character/representImage/" +get_charName() + "_f.bmp");
		
		_represent.setPosition(get_position().x,get_position().y);
		addChild(_represent,3);
		}
		else
		{
			_charName = "";
			_nameCharacter.setString("");
		}
	}

	public String get_charName() {
		return _charName;
	}

	public void set_charName(String _charName) {
		this._charName = _charName;
	}

	public int get_teamID() {
		return _teamID;
	}

	public void set_teamID(int _teamID) {
		this._teamID = _teamID;
	}

	public CGPoint get_position() {
		return _position;
	}

	public void set_position(CGPoint _position) {
		this._position = _position;
	}

	public String get_controler() {
		return _controler;
	}

	public void set_controler(String _controler) {
		this._controler = _controler;
		_markPlayer.setString(_controler);
	}

	
}
